package com.mars.admin.modules.app;

import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.post.entity.TopicFollow;
import com.mars.admin.modules.post.service.ITopicFollowService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 话题关注控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/topic-follow")
@Tag(name = "话题关注管理", description = "话题关注相关接口")
public class TopicFollowController {

    @Autowired
    private ITopicFollowService topicFollowService;

    /**
     * 关注话题
     */
    @PostMapping("/follow")
    @Operation(summary = "关注话题", description = "用户关注指定话题")
    public Result<Boolean> followTopic(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "话题ID") @RequestParam Long topicId) {
        try {
            boolean success = topicFollowService.followTopic(userId, topicId);
            if (success) {
                return Result.success("关注成功", true);
            } else {
                return Result.error("关注失败，可能已经关注过了");
            }
        } catch (Exception e) {
            log.error("关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            return Result.error("关注失败：" + e.getMessage());
        }
    }

    /**
     * 取消关注话题
     */
    @PostMapping("/unfollow")
    @Operation(summary = "取消关注话题", description = "用户取消关注指定话题")
    public Result<Boolean> unfollowTopic(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "话题ID") @RequestParam Long topicId) {
        try {
            boolean success = topicFollowService.unfollowTopic(userId, topicId);
            if (success) {
                return Result.success("取消关注成功", true);
            } else {
                return Result.error("取消关注失败");
            }
        } catch (Exception e) {
            log.error("取消关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            return Result.error("取消关注失败：" + e.getMessage());
        }
    }

    /**
     * 检查是否关注话题
     */
    @GetMapping("/is-following")
    @Operation(summary = "检查是否关注话题", description = "检查用户是否关注了指定话题")
    public Result<Boolean> isFollowing(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "话题ID") @RequestParam Long topicId) {
        try {
            boolean isFollowing = topicFollowService.isFollowing(userId, topicId);
            return Result.success(isFollowing);
        } catch (Exception e) {
            log.error("检查是否关注话题失败，用户ID：{}，话题ID：{}", userId, topicId, e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }

    /**
     * 查询用户关注的话题列表
     */
    @GetMapping("/user-followed")
    @Operation(summary = "查询用户关注的话题", description = "分页查询用户关注的话题列表")
    public Result<Page<TopicFollow>> getUserFollowedTopics(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<TopicFollow> page = new Page<>(pageNumber, pageSize);
            Page<TopicFollow> result = topicFollowService.getUserFollowedTopics(userId, page);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询用户关注的话题列表失败，用户ID：{}", userId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询话题的关注用户列表
     */
    @GetMapping("/topic-followers")
    @Operation(summary = "查询话题的关注用户", description = "分页查询话题的关注用户列表")
    public Result<Page<TopicFollow>> getTopicFollowers(
            @Parameter(description = "话题ID") @RequestParam Long topicId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<TopicFollow> page = new Page<>(pageNumber, pageSize);
            Page<TopicFollow> result = topicFollowService.getTopicFollowers(topicId, page);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询话题的关注用户列表失败，话题ID：{}", topicId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询用户关注的话题ID列表
     */
    @GetMapping("/user-followed-ids")
    @Operation(summary = "查询用户关注的话题ID", description = "查询用户关注的话题ID列表")
    public Result<List<Long>> getUserFollowedTopicIds(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            List<Long> topicIds = topicFollowService.getUserFollowedTopicIds(userId);
            return Result.success(topicIds);
        } catch (Exception e) {
            log.error("查询用户关注的话题ID列表失败，用户ID：{}", userId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询话题的关注用户ID列表
     */
    @GetMapping("/topic-follower-ids")
    @Operation(summary = "查询话题的关注用户ID", description = "查询话题的关注用户ID列表")
    public Result<List<Long>> getTopicFollowerIds(
            @Parameter(description = "话题ID") @RequestParam Long topicId) {
        try {
            List<Long> userIds = topicFollowService.getTopicFollowerIds(topicId);
            return Result.success(userIds);
        } catch (Exception e) {
            log.error("查询话题的关注用户ID列表失败，话题ID：{}", topicId, e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 统计话题的关注数量
     */
    @GetMapping("/topic-followers-count")
    @Operation(summary = "统计话题的关注数量", description = "统计指定话题的关注数量")
    public Result<Integer> countTopicFollowers(
            @Parameter(description = "话题ID") @RequestParam Long topicId) {
        try {
            int count = topicFollowService.countTopicFollowers(topicId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计话题的关注数量失败，话题ID：{}", topicId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 统计用户关注的话题数量
     */
    @GetMapping("/user-followed-count")
    @Operation(summary = "统计用户关注的话题数量", description = "统计用户关注的话题数量")
    public Result<Integer> countUserFollowedTopics(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        try {
            int count = topicFollowService.countUserFollowedTopics(userId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("统计用户关注的话题数量失败，用户ID：{}", userId, e);
            return Result.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 查询热门关注的话题
     */
    @GetMapping("/hot-followed")
    @Operation(summary = "查询热门关注的话题", description = "查询热门关注的话题ID列表")
    public Result<List<Long>> getHotFollowedTopics(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Long> topicIds = topicFollowService.getHotFollowedTopicIds(limit);
            return Result.success(topicIds);
        } catch (Exception e) {
            log.error("查询热门关注的话题失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 批量关注话题
     */
    @PostMapping("/batch-follow")
    @Operation(summary = "批量关注话题", description = "用户批量关注多个话题")
    public Result<Boolean> batchFollowTopics(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "话题ID列表") @RequestBody List<Long> topicIds) {
        try {
            if (topicIds == null || topicIds.isEmpty()) {
                return Result.error("话题ID列表不能为空");
            }
            
            int successCount = 0;
            for (Long topicId : topicIds) {
                try {
                    if (topicFollowService.followTopic(userId, topicId)) {
                        successCount++;
                    }
                } catch (Exception e) {
                    log.warn("批量关注话题时单个关注失败，用户ID：{}，话题ID：{}", userId, topicId, e);
                }
            }
            
            if (successCount > 0) {
                return Result.success("批量关注成功，成功关注" + successCount + "个话题", true);
            } else {
                return Result.error("批量关注失败");
            }
        } catch (Exception e) {
            log.error("批量关注话题失败，用户ID：{}，话题ID列表：{}", userId, topicIds, e);
            return Result.error("批量关注失败：" + e.getMessage());
        }
    }

    /**
     * 批量取消关注话题
     */
    @PostMapping("/batch-unfollow")
    @Operation(summary = "批量取消关注话题", description = "用户批量取消关注多个话题")
    public Result<Boolean> batchUnfollowTopics(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "话题ID列表") @RequestBody List<Long> topicIds) {
        try {
            if (topicIds == null || topicIds.isEmpty()) {
                return Result.error("话题ID列表不能为空");
            }
            
            int successCount = 0;
            for (Long topicId : topicIds) {
                try {
                    if (topicFollowService.unfollowTopic(userId, topicId)) {
                        successCount++;
                    }
                } catch (Exception e) {
                    log.warn("批量取消关注话题时单个取消失败，用户ID：{}，话题ID：{}", userId, topicId, e);
                }
            }
            
            if (successCount > 0) {
                return Result.success("批量取消关注成功，成功取消关注" + successCount + "个话题", true);
            } else {
                return Result.error("批量取消关注失败");
            }
        } catch (Exception e) {
            log.error("批量取消关注话题失败，用户ID：{}，话题ID列表：{}", userId, topicIds, e);
            return Result.error("批量取消关注失败：" + e.getMessage());
        }
    }
} 